﻿#region License and Copyright Notice

//The Formula 1 Season is a Windows Mobile application that lets you keep track 
// of all major happenings and statistics during a Formula 1 season 

//Copyright (c) 2008 Rajaram B., Jeevan James, Ananth B.
//All rights reserved.

//The contents of this file are made available under the terms of the
//Mozilla Public License v1.1 (the "License") which accompanies this
//distribution, and is available at the following URL:
//http://www.opensource.org/licenses/mozilla1.1.php

//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
//the specific language governing rights and limitations under the License.

//By using this software in any fashion, you are agreeing to be bound by the
//terms of the License.

#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Formula1Season.Client.Controls
{
    public enum Align
    {
        TopLeft, TopCenter, TopRight,
        MiddleLeft, MiddleCenter, MiddleRight,
        BottomLeft, BottomCenter, BottomRight
    }
    
    public partial class ThemeButton : Control
    {
        private Align _align = Align.MiddleCenter;
        
        protected override void OnPaint(PaintEventArgs e)
        {
            // Don't call base.OnPaint!
            e.Graphics.DrawImage(ThemeManager.CurrentTheme.ButtonBackground,
                e.ClipRectangle,
                new Rectangle(0, 0, ThemeManager.CurrentTheme.ButtonBackground.Width,
                    ThemeManager.CurrentTheme.ButtonBackground.Height), GraphicsUnit.Pixel);

            // Draw the  text, if any
            if (Text != string.Empty)
            {
                int x = 0;
                int y = 0;
                SizeF size;
                switch (Align)
                {
                    case Align.TopLeft:
                        x = -1;

                        break;

                    case Align.TopCenter:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width / 2 - (int)size.Width / 2;

                        break;

                    case Align.TopRight:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width - (int)size.Width;

                        break;
                    
                    case Align.MiddleLeft:
                        x = -1;
                        size = e.Graphics.MeasureString(Text, Font);
                        y = Height / 2 - (int)size.Height / 2;

                        break;

                    case Align.MiddleCenter:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width / 2 - (int)size.Width / 2;
                        y = Height / 2 - (int)size.Height / 2;

                        break;

                    case Align.MiddleRight:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width - (int)size.Width;
                        y = Height / 2 - (int)size.Height / 2;

                        break;

                    case Align.BottomLeft:
                        x = -1;
                        size = e.Graphics.MeasureString(Text, Font);
                        y = Height - (int)size.Height;

                        break;

                    case Align.BottomCenter:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width / 2 - (int)size.Width / 2;
                        y = Height - (int)size.Height;

                        break;

                    case Align.BottomRight:
                        size = e.Graphics.MeasureString(Text, Font);
                        x = Width - (int)size.Width;
                        y = Height - (int)size.Height;

                        break;
                }

                e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), (float)x, (float)y);
            }
        }
        
        public ThemeButton()
        {
            Width = 120;
            Height = 50;
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                Invalidate();
            }
        }

        public Align Align
        {
            get
            {
                return _align;
            }
            set
            {
                _align = value;
                Invalidate();
            }
        }
    }
}